#include <stdio.h>
#include <stdlib.h>

#define MAX_FILENAME_SIZE 256
#define ERROR_MSG_MAX_SIZE 20 + MAX_FILENAME_SIZE

long get_number_of_bytes(const char *fpath);
void check_args(int argc, char **argv);



int main(int argc, char *argv[]){
    check_args(argc, argv);
    const char *fpath = argv[1];
    long l = get_number_of_bytes(fpath);
    if(l == -1)
        return 1;
    printf("%s is %ld bytes long\n", fpath, l);
}

long get_number_of_bytes(const char *fpath)
{
    FILE *f = fopen(fpath, "rb");
    if(!f)
    {
        char errormsg[ERROR_MSG_MAX_SIZE] = {0};
        sprintf(errormsg, "Could not open file %s", fpath);
        perror(errormsg);
        return -1;
    }
    //fseek(f, 0, SEEK_END);
    //long l = ftell(f);
    //fseek(f, 0, SEEK_SET);
    char c;
    long l = 0;
    for(;fread(&c, 1, sizeof(char), f); ++l);
    fclose(f);
    return l;
}

void check_args(int argc, char **argv)
{
    if(argc != 2)
    {
        fprintf(stderr,
                "Usage: %s <file>\n"
                "ERROR: no file provided\n"
                , argv[0]);
        exit(1);
    }
}
